{
 "cells": [
  {
   "cell_type": "raw",
   "id": "a1915c573ecefe5e",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "---\n",
    "sidebar_label: Upstage\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c07bf6cf93adec81",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# UpstageEmbeddings\n",
    "\n",
    "This notebook covers how to get started with Upstage embedding models.\n",
    "\n",
    "## Installation\n",
    "\n",
    "Install `langchain-upstage` package.\n",
    "\n",
    "```bash\n",
    "pip install -U langchain-upstage\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2b1c8fd01d71683",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Environment Setup\n",
    "\n",
    "Make sure to set the following environment variables:\n",
    "\n",
    "- `UPSTAGE_API_KEY`: Your Upstage API key from [Upstage console](https://console.upstage.ai/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a50c04f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"UPSTAGE_API_KEY\"] = \"YOUR_API_KEY\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c02e30aa",
   "metadata": {},
   "source": [
    "\n",
    "## Usage\n",
    "\n",
    "Initialize `UpstageEmbeddings` class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea89ac9da2520b91",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from langchain_upstage import UpstageEmbeddings\n",
    "\n",
    "embeddings = UpstageEmbeddings(model=\"solar-embedding-1-large\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8be6eab1",
   "metadata": {},
   "source": [
    "Use `embed_documents` to embed list of texts or documents. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26aa179f7ad60cbe",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "doc_result = embeddings.embed_documents(\n",
    "    [\"Sung is a professor.\", \"This is another document\"]\n",
    ")\n",
    "print(doc_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0197135c",
   "metadata": {},
   "source": [
    "Use `embed_query` to embed query string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a80d47413c27bbc",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "query_result = embeddings.embed_query(\"What does Sung do?\")\n",
    "print(query_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d5ff58e",
   "metadata": {},
   "source": [
    "Use `aembed_documents` and `aembed_query` for async operations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af75139d0e1d9ba2",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# async embed query\n",
    "await embeddings.aembed_query(\"My query to look up\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17968d20c0dfb2f9",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# async embed documents\n",
    "await embeddings.aembed_documents(\n",
    "    [\"This is a content of the document\", \"This is another document\"]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6429f2f8",
   "metadata": {},
   "source": [
    "## Using with vector store\n",
    "\n",
    "You can use `UpstageEmbeddings` with vector store component. The following demonstrates a simple example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09ac41d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.vectorstores import DocArrayInMemorySearch\n",
    "\n",
    "vectorstore = DocArrayInMemorySearch.from_texts(\n",
    "    [\"harrison worked at kensho\", \"bears like to eat honey\"],\n",
    "    embedding=UpstageEmbeddings(model=\"solar-embedding-1-large\"),\n",
    ")\n",
    "retriever = vectorstore.as_retriever()\n",
    "docs = retriever.invoke(\"Where did Harrison work?\")\n",
    "print(docs)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
